package com.xianggu.mybatis.headlerImpl;

import com.xianggu.mybatis.utils.TypeHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * @author chengjy
 * @date 2025/3/5
 */
public class DateUtilTypeHandler implements TypeHandler<Date> {
    @Override
    public void setPreparedStatementValue(PreparedStatement preparedStatement, int i, Date o, String pName) {
        try {
            preparedStatement.setDate(i , (java.sql.Date) o);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Date getParameterValue(ResultSet resultSet, String columnName) {
        try {
            return resultSet.getDate( columnName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Date toBaseType(Object num) {
        return "".equals(String.valueOf(num)) ? null : new Date(Long.parseLong(String.valueOf(num)));
    }
}
